<?php

namespace App\Admin\Actions\Post;

use App\Http\Controllers\Api\WechatPayController;
use App\Models\User;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Refund extends RowAction
{
    public $name = '退款';

    public function handle(Model $model,Request $request)
    {


        $openid= User::where('id',$model->user_id)->value('openid');

        if(!$openid)
            return $this->response()->error('退款失败')->refresh();

        $wechatPay = new WechatPayController();

        $result = $wechatPay->refund(['order_sn'=>$model->order_sn,'money'=>$model->money]);

        if($result['return_code']==="SUCCESS"&&$result['result_code']==="SUCCESS"){

            $model->order_status = 3;
            $model->refund_note = $request->get('refund_note');

            $res = $model->save();

            return $this->response()->success('退款成功')->refresh();

        }else{

            return $this->response()->error($result['err_code_des'])->refresh();

        }





    }


//    public function dialog()
//    {
//        $this->confirm('确定退款？');
//    }

    public function form(){
        $this->text('refund_note','退款备注');
    }

}
